package com.jiefeng.manage.web.controller.dataexcel;

import com.jiefeng.manager.service.dataexcel.DataExcelService;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * Created by WuZhiqi on 2016/5/30.
 */
@Controller
public class ObjectDataExcelImportCtrl {

    private static final Logger logger = LoggerFactory.getLogger(ObjectDataExcelImportCtrl.class);

    /**
     * 匹配数据Excel导入
     * @param response
     */
    @RequestMapping("matchedDataExcelTemplate1")
    public void exportExcel(HttpServletResponse response, @RequestParam(value = "excelFile") CommonsMultipartFile excelFile) {
        if(!excelFile.isEmpty()){
            logger.info("文件上传成功: " + excelFile.getFileItem().getFieldName());
            try{
                DiskFileItem diskFileItem = (DiskFileItem)excelFile.getFileItem();
                File file = diskFileItem.getStoreLocation();
                DataExcelService<Object> dataExcelService = DataExcelService.getInstance(Object.class);
                dataExcelService.importExcel(file);
            }catch (Exception e){
                logger.error("文件上传导入Excel异常!", e);
            }
        }
    }
}
